<template>
  <div class="header">
    <div class="page-title">资讯管理</div>
    <el-button
        class="m-t-15"
        type="primary"
        size="small"
        icon="el-icon-edit"
        @click="handleCCreate"
    >资讯列表</el-button
    >
    <el-form>
      <el-input type="hidden" v-model="form.id"></el-input>
      <el-form-item label="标题">
        <el-input
            v-model="form.info_title"
            autocomplete="off"
            placeholder="请输入标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input
            type="textarea"
            :rows="2"
            v-model="form.info_describe"
            autocomplete="off"
            placeholder="请输入描述"
        ></el-input>
      </el-form-item>
      <el-form-item label="内容">
        <Editor :value="form.info_content" @change="changeEditor" style="width:75%"></Editor>
      </el-form-item>
    </el-form>
  </div>

    <div class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmitForm">确 定</el-button>
    </div>

</template>
<script>
import Editor from '@/components/Editor'
import { UPLOAD_URL,CONST_DMOMAIN_URL } from "@/config/env";
import {
  request_information_list,
  request_information_create,
  request_information_update,
  request_information_delete,
  request_information_detail,
} from "@/util/api";
export default {
  name: "Hotel",
  data() {
    return {
      current: 1,
      total: 0,
      form: {
      },
      // 门店是否显示
      uploadUrl: UPLOAD_URL,
      actionType: 0, // 1 添加  2 编辑
    };
  },
  components: {
    Editor
  },
  //computed: {},
  mounted() {
    let id = this.$route.query.id;
    if(id){
      this.actionType=2
      request_information_detail( {
        id:id
      }).then(res => {
        const result = res.data
        if (result.code == 200) {
          this.form = result.data
        }
      }).catch()
    }else{
      this.actionType=1
    }
  },
  methods: {
    // 富文本内容
    changeEditor(val) {
      this.form.info_content = val
    },
    removeBanner(key){
      this.$confirm("确定要删除此图片吗？", "系统提示", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          this.form.banners.splice(key, 1)
        })
        
    },
    handleStation(row) {
        this.$router.push('/hall?id=' + row.id)
    },
     //发布图片上传成功的回调函数
    handleUploadSuccess(res) {
      this.form.hotel_image_url = CONST_DMOMAIN_URL + res.data;
    },
    
    handleUploadSuccessByBanners(res,raw, key) {
      this.form.banners[key] = CONST_DMOMAIN_URL + res.data;
    },
    //图片上传前的验证函数
    beforeAvatarUpload(file) {
      let types = ['image/jpeg', 'image/jpg', 'image/gif', 'image/bmp', 'image/png'];
      const isJPG=types.includes(file.type)
      // const isJPG = file.type === "image/png";验证单张图片
      const isLt2M = file.size / 1024 / 1024 < 3;
      if (!isJPG) {
        this.$message.error("上传图片只能是 jpg,jpeg,bmp,png 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 3MB!");
      }
      return isJPG && isLt2M;
    },

    // 添加
    handleCCreate() {
      this.actionType = 1;
      this.$router.push('/information')
    },
    // 编辑
    handleEdit(row) {
      this.actionType = 2;
      this.form = row;
    },
    // 提交
    handleSubmitForm() {
      let apiFunc;
      switch (this.actionType) {
        case 1: // 添加
            apiFunc = request_information_create;
          break;
        case 2: // 编辑
            apiFunc = request_information_update;
          break;
        default:
          this.$message.error({
            message: "操作类型不正确",
            offset: 70,
          });
          return false;
      }
      apiFunc({
        ...this.form,
      }).then((res) => {
        const result = res.data;
        if (result.code == 200) {
          this.$notify.success({
            title: "系统提示",
            message: result.message,
            offset: 70,
          });
          this.$router.push('/information')
        } else {
          this.$message.error({
            message: result.message,
            offset: 70,
          });
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除，是否继续？", "系统提示", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          request_information_delete({
            id: row.id,
          })
            .then((res) => {
              const result = res.data;
              if (result.code == 200) {
                this.$message.success({
                  message: "删除成功！",
                  offset: 70,
                });
                this.changePage(this.page)
              } else {
                this.$message.error({
                  message: result.message,
                  offset: 70,
                });
              }
            })
            .catch();
        })
        .catch(() => {
          this.$message.info({
            message: "已取消删除！",
            offset: 70,
          });
        });
    },
    // 分页
    changePage(page) {
      request_information_list({
        page,
      })
        .then((res) => {
          const result = res.data;
          if (result.code == 200) {
            this.tableData = result.data.data;
            this.total = result.data.total;
            this.current = result.data.current;
          }
        })
        .catch();
    },
  },
};
</script>

<style scoped  lang="less">
  .avatar-uploader {
    float:left;
    margin-right:10px;
    margin-bottom:10px;
   }
   .banner-add {
      clear:both;
      margin-top:20px;
   }
</style>